package com.zc.controller;

import com.zc.utils.PropertisConfigTest;
import com.zc.pojo.PojoTest;
import com.zc.timer.AsyncTaskTest;
import com.zc.utils.JSONResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Map;

/**
 * 1.Controller测试类
 */

//@Controller
@RestController
@RequestMapping("hello")
@Slf4j
public class ControllerTest {

    @RequestMapping("hello")
    //@ResponseBody
    //@GetMapping("hello1")
    //@PostMapping("hello1")
    //@DeleteMapping("hello1")
    //@PutMapping("hello1")
    public String hello() {
        return "Hello Woid 2 ~~";
    }

    /**
     * 读取bean容器的数据
     */
    @Autowired
    private PojoTest pojoTest;

    @GetMapping("getPojoTest")
    public PojoTest getPojoTest() {
        return pojoTest;
    }

    /**
     * 读取propertis配置文件中的数据
     */
    @Autowired
    private PropertisConfigTest propertisConfigTest;

    @RequestMapping("geMyConfig")
    public PropertisConfigTest geMyConfig() {
        return propertisConfigTest;
    }

    /**
     * 通过表达式读取yml自定义的配置数据
     */

    @Value("${zc.name}")
    private String name;
    @Value("${zc.age}")
    private Integer age;
    @Value("${zc.sex}")
    private String sex;
    @Value("${zc.port}")
    private String port;

    @RequestMapping("geYmlConfig")
    public Object geYmlConfig() {
        return name + ";\t" + age + ";\t" + sex + ";\t" + port;
    }


    /**
     * lombok相关注解使用：（注意：一个项目中只要有一个人使用，其他人也必须安装该插件使用，否则项目编译会不通过）
     *
     * @Slf4j 打印日志使用
     * @Data
     * @ToString
     * @NoArgsConstructor
     * @AllArgsConstructor
     */
    @RequestMapping("gePojoTestLombok")
    public JSONResult gePojoTestLombok() {
        PojoTest pojoTest2 = new PojoTest(1, "liushan", 1);
        log.debug(pojoTest2.toString());
        log.info(pojoTest2.toString());
        log.warn(pojoTest2.toString());
        log.error(pojoTest2.toString());
        //return name + ";\t" + age + ";\t" + sex + ";\t" + port;
        //return JSONResult.errorMsg("调用接口错误");
        return JSONResult.ok(pojoTest2);
    }

    /**
     * http://127.0.0.1:8090/hello/18/get?id=1&name=zancong
     */
    @GetMapping("{age}/get")
    public String getPojoTest(@PathVariable("age") String age,
                              @RequestParam Integer id,
                              @RequestParam String name) {
        /**
         * @RequestParam:用于获取url中的请求参数，如果参数变量名保持一致，该注解可以省略
         *
         */
        log.info("age=" + age);
        log.info("id=" + id);
        log.info("name=" + name);

        return "查询~";
    }

    /**
     * http://127.0.0.1:8090/hello/creat
     *
     * @return
     */
    @PostMapping("creat")
    public String creatPojoTest(@RequestBody Map<String, Object> map,
                                @RequestHeader("token") String token,
                                //@CookieValue("clientId") String clientId,
                                HttpServletRequest request) {

        log.info("token=" + token);
        //log.info("clientId=" + clientId);
        log.info("map=" + map.toString());

        String headerToken = request.getHeader("token");
        log.info("headerToken=" + headerToken);


        return "新增~";
    }

    /**
     * 文件上传样例
     */

    @Value("${zc.uploadfilepath}")
    private String uploadFilePath;

    @PostMapping("upload")
    public JSONResult upload(MultipartFile file) throws IOException {

        //获取文件保存路径：D:\ZCproject\zc-springboot-demo/uploadFile
        String currentPath = System.getProperty("user.dir") + "/" + uploadFilePath;//uploadFilePath;

        log.info("currentPath=" + currentPath);
        log.info("filename=" + file.getOriginalFilename());
        file.transferTo(new File(currentPath + "/" + file.getOriginalFilename()));

        log.info("upload=" + "上传成功");

        return JSONResult.ok();

    }


    /**
     * 调用异步处理
     *
     * @Slf4j 打印日志使用
     * @Data
     * @ToString
     * @NoArgsConstructor
     * @AllArgsConstructor
     */
    @Autowired
    private AsyncTaskTest asyncTaskTest;

    @RequestMapping("geAsync")
    public JSONResult geAsync() {
        asyncTaskTest.publishMsg();
        log.warn("跳过异步任务处理！");
        return JSONResult.ok("跳过异步任务处理");
    }


}
